package com.eliza.base.views.learn.widget

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.widget.Toast
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.appwidget.CheckBox
import androidx.glance.appwidget.CheckboxDefaults
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.appWidgetBackground
import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.provideContent
import androidx.glance.background
import androidx.glance.color.ColorProvider
import androidx.glance.layout.Alignment
import androidx.glance.layout.Column
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.height
import androidx.glance.layout.padding
import androidx.glance.text.FontStyle
import androidx.glance.text.FontWeight
import androidx.glance.text.TextStyle
import com.eliza.base.R
import kotlin.random.Random

//xml -->xml -->Receiver 小部件其实就是一个 BroadcastReceiver
// 扩展接收器
class MyAppWidgetReceiver : GlanceAppWidgetReceiver() {
    override val glanceAppWidget: GlanceAppWidget = MyAppWidget()
}


class MyAppWidget : GlanceAppWidget() {
    override val sizeMode: SizeMode
        get() = SizeMode.Exact

    override suspend fun provideGlance(context: Context, id: GlanceId) {
        provideContent {
            // create your AppWidget here
            GlanceTheme {
                WidgetX.Show(context, id)
            }
        }
    }

    @Composable
    private fun MyContentX() {
        Column(
            modifier = GlanceModifier.fillMaxSize().background(Color.LightGray)
                .padding(R.dimen.external_padding).cornerRadius(R.dimen.corner_radius)
                .appWidgetBackground(),
            verticalAlignment = Alignment.Vertical.CenterVertically,
            horizontalAlignment = Alignment.Horizontal.CenterHorizontally
        ) {
            val textStyle = TextStyle(
                color = ColorProvider(day = Color.Red, night = Color.Cyan),
                fontSize = 16.sp,
                fontWeight = FontWeight.Bold,
                fontStyle = FontStyle.Italic
            )
            val fillModifier = GlanceModifier.fillMaxWidth()

            var checkbox1Checked by remember { mutableStateOf(false) }
            var checkbox2Checked by remember { mutableStateOf(false) }
            var checkbox3Checked by remember { mutableStateOf(false) }
            var switch1Checked by remember { mutableStateOf(false) }
            var switch2Checked by remember { mutableStateOf(false) }

            @Suppress("AutoboxingStateCreation")
            var radioChecked by remember { mutableStateOf(0) }

            CheckBox(
                checked = checkbox1Checked,
                onCheckedChange = { checkbox1Checked = !checkbox1Checked },
                text = "Checkbox 1",
                modifier = GlanceModifier.height(56.dp).padding(bottom = 24.dp),
            )
            CheckBox(
                checked = checkbox2Checked,
                onCheckedChange = { checkbox2Checked = !checkbox2Checked },
                text = "Checkbox 2",
                style = textStyle,
                modifier = fillModifier,
                colors = CheckboxDefaults.colors(
                    checkedColor = ColorProvider(day = Color.Red, night = Color.Cyan),
                    uncheckedColor = ColorProvider(day = Color.Green, night = Color.Magenta)
                )
            )
        }
//    @Composable
//    private fun MyContent() {
//
//        /*
//        *
//        * 状态的概念可分为以下几个方面：
//        应用状态：widget 所需的应用状态或内容。例如，由用户定义的存储目的地（即数据库）的列表。
//        概览状态：仅与应用 widget 相关的
//    * */
//        // Widget 状态
////        val repository = remember { DestinationsRepository.getInstance() }
////         Retrieve the cache data everytime the content is refreshed
////        val destinations by repository.destinations.collectAsState(State.Loading)
//        val string = LocalContext.current.getString(R.string.app_info)
//        Column(
//            modifier = GlanceModifier.fillMaxSize()
//                .background(GlanceTheme.colors.background),
//            verticalAlignment = Alignment.Top,
//            horizontalAlignment = Alignment.CenterHorizontally
//        ) {
//            Text(text = "Where to?", modifier = GlanceModifier.padding(12.dp))
//            Row(horizontalAlignment = Alignment.CenterHorizontally) {
//                Text(text = "LocalContext $string")
//                Button(
//                    text = "MyAppWidgetActivity",
//                    // 点击互动
//                    onClick = actionStartActivity<MyAppWidgetActivity>(
//                        actionParametersOf(destinationKey to "home")
//                    )
//                )
//                Button(
//                    text = "MainActivity",
//                    onClick = actionStartActivity<MainActivity>()
//                )
//                Image(
//                    provider = androidx.glance.ImageProvider(R.drawable.tou_xiang),
//                    modifier = GlanceModifier.clickable(
//                        onClick = actionRunCallback<RefreshAction>()
//                    ),
//                    contentDescription = "Refresh"
//                )
//                Button(
//                    text = "Sync",
//                    onClick = actionStartService<MyAppWidgetService>(
//                        isForegroundService = true // define how the service is launched
//                    )
//                )
//                Button(
//                    text = "MyBroadcastReceiver",
//                    onClick = actionSendBroadcast<MyBroadcastReceiver>()
//                )
//                Text(
//                    text = "submitData",
//                    modifier = GlanceModifier.clickable {
//
//                    }
//                )
////                val LocalContext = staticCompositionLocalOf<Context> { error("No default context") }
////                Button(
////                    text = "click toast",
////                    onClick = {
////                        Toast.makeText(LocalContext.current, "Toast message", Toast.LENGTH_SHORT)
////                            .show()
////                    })
//            }
//        }
//    }
    }

    class MyAppWidgetService : Service() {
        // Binder given to clients.
        private val binder = LocalBinder()

        inner class LocalBinder : Binder() {
            // Return this instance of LocalService so clients can call public methods.
            fun getService(): MyAppWidgetService = this@MyAppWidgetService
        }

        val randomNumber: Int
            get() = Random.nextInt(90)

        override fun onBind(intent: Intent): IBinder {
            Toast.makeText(baseContext, "MyAppWidgetService", Toast.LENGTH_SHORT).show()
            return binder
        }
    }
}
